package com.ly.heart.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ly.common.core.domain.AjaxResult;
import com.ly.common.exception.CustomException;
import com.ly.common.utils.DateUtils;
import com.ly.common.utils.SecurityUtils;
import com.ly.common.utils.StringUtils;
import com.ly.common.utils.erp.ConstUtil;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.domain.CustomerEntity;
import com.ly.heart.domain.KingInvoiceTypeMappingEntity;
import com.ly.heart.domain.dto.StartProcessDto;
import com.ly.heart.mapper.KingInvoiceTypeMappingMapper;
import com.ly.heart.service.IActModelService;
import com.ly.heart.service.IKingInvoiceTypeMappingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 发票类型映射 服务实现类
 * </p>
 * @author WuZH
 * @since 2024-08-18
 */
@Service
public class KingInvoiceTypeMappingServiceImpl extends ServiceImpl<KingInvoiceTypeMappingMapper, KingInvoiceTypeMappingEntity> implements IKingInvoiceTypeMappingService {

    @Resource
    private IActModelService iActModelService;

    @Override
    public List<KingInvoiceTypeMappingEntity> queryList (KingInvoiceTypeMappingEntity kingInvoiceTypeMappingEntity) {
        return null;
    }

    @Override
    public AjaxResult draft (KingInvoiceTypeMappingEntity kingInvoiceTypeMappingEntity) {
        kingInvoiceTypeMappingEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
        kingInvoiceTypeMappingEntity.setCreateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
        kingInvoiceTypeMappingEntity.setCreateTime ( DateUtils.getNowDate () );
        kingInvoiceTypeMappingEntity.setState ( ConstUtil.STATE_IN_DRAFT );
        boolean save = this.save ( kingInvoiceTypeMappingEntity );
        if ( ! save ) {
            throw new CustomException ( "暂存发票类型映射失败！" );
        }
        return AjaxResult.success ( "暂存发票类型映射成功", kingInvoiceTypeMappingEntity.getId () );
    }

    @Override
    public AjaxResult submit (KingInvoiceTypeMappingEntity kingInvoiceTypeMappingEntity) {
        if ( StringUtils.isBlank ( kingInvoiceTypeMappingEntity.getId () ) ) {
            return AjaxResult.error ( "请先暂存该单据！" );
        }
        StartProcessDto startDto = new StartProcessDto ().
                setBusinessId ( kingInvoiceTypeMappingEntity.getId () ).
                setBusinessType ( kingInvoiceTypeMappingEntity.getBusinessType () ).setModelId ( kingInvoiceTypeMappingEntity.getModelId () ).
                setBusinessName ( "发票类型映射".concat ( kingInvoiceTypeMappingEntity.getId () ) );
        String instanceId = iActModelService.startProcess ( startDto );
        boolean update = this.update ( new LambdaUpdateWrapper<KingInvoiceTypeMappingEntity> ().
                set ( KingInvoiceTypeMappingEntity :: getState, ConstUtil.STATE_IN_PROCESS ).
                set ( KingInvoiceTypeMappingEntity :: getInstanceId, instanceId ).
                eq ( KingInvoiceTypeMappingEntity :: getId, kingInvoiceTypeMappingEntity.getId () ) );
        if ( ! update ) {
            throw new CustomException ( "提交发票类型映射失败！" );
        }
        return AjaxResult.success ( "提交发票类型映射成功" );
    }

    @Override
    public AjaxResult updateSingle (KingInvoiceTypeMappingEntity kingInvoiceTypeMappingEntity) {
        kingInvoiceTypeMappingEntity.setUpdateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
        kingInvoiceTypeMappingEntity.setUpdateTime ( DateUtils.getNowDate () );
        boolean b = this.updateById ( kingInvoiceTypeMappingEntity );
        if ( ! b ) {
            throw new CustomException ( "修改发票类型映射失败！" );
        }
        return AjaxResult.success ( "修改发票类型映射成功" );
    }

    @Override
    public AjaxResult deleteSingle (String[] ids) {
        boolean update = this.update ( new LambdaUpdateWrapper<KingInvoiceTypeMappingEntity> ().set ( KingInvoiceTypeMappingEntity :: getIsUsed, ConstUtil.IS_USED_NO ).
                set ( KingInvoiceTypeMappingEntity :: getUpdateBy, SecurityUtils.getLoginUser ().getUser ().getUserId () ).
                set ( KingInvoiceTypeMappingEntity :: getUpdateTime, DateUtils.getNowDate () ).
                in ( KingInvoiceTypeMappingEntity :: getId, ids ) );
        if ( ! update ) {
            throw new CustomException ( "删除发票类型映射失败！" );
        }
        return AjaxResult.success ( "删除发票类型映射成功" );
    }

    @Override
    public AjaxResult disabled (String id) {
        KingInvoiceTypeMappingEntity invoiceTypeMappingEntity = this.getById ( id );
        boolean update = this.update ( new LambdaUpdateWrapper<KingInvoiceTypeMappingEntity> ().
                set ( KingInvoiceTypeMappingEntity :: getIsDisabled, invoiceTypeMappingEntity.getIsDisabled () == 1 ? 2 : 1 ).
                set ( KingInvoiceTypeMappingEntity :: getDisabledBy, SecurityUtils.getLoginUser ().getUser ().getUserId () ).
                set ( KingInvoiceTypeMappingEntity :: getDisabledTime, DateUtils.getNowDate () ).
                eq ( KingInvoiceTypeMappingEntity :: getId, id ) );
        if ( ! update ) {
            throw new CustomException ( invoiceTypeMappingEntity.getIsDisabled () == 1 ? "禁用发票类型映射失败！" : "解禁发票类型映射失败！" );
        }
        return AjaxResult.success ( invoiceTypeMappingEntity.getIsDisabled () == 1 ? "禁用发票类型映射成功" : "解禁发票类型映射成功" );
    }
}
